﻿using Bll;
using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        public class Test
        {
            public int TestId { get; set; }
            public string Name { get; set; }
            public DateTime CreateDate { get; set; }
        }

        /// <summary>
        /// 测试模板引擎帮助类
        /// </summary>
        [TestMethod]
        public void TestTemplateHelper()
        {
            string result = "";

            #region AderTemplates
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"RequestTemplate\Test.xml");
            Dictionary<string, object> param = new Dictionary<string, object>()
            {
                {"Test",new Test() { TestId = 222, Name = "名字", CreateDate = DateTime.Now }},
                { "StringExtension",typeof(Common.StringExtension)}//使用当前应用程序加载以外的类静态方法，需要声明类类型，不然模板无法识别类型（并且模板只支持.Net 2.0，高级的类型，关键字等不识别）
            };
            result = TemplateHelper.RunTemplate(path, param);
            #endregion

            Console.ReadKey();
        }

        /// <summary>
        /// 测试本地配置和文件日志帮助类
        /// </summary>
        [TestMethod]
        public void TestFileLogHelper()
        {
            var obj = LocalConfigHelper.GetValue("Test", "Key1", "测试");
            var obj2 = LocalConfigHelper.GetValue("Test", "Key2", "测试");
            var obj3 = LocalConfigHelper.GetValue("Test", "Key3", "测试");

            FileLogHelper.DebugAdd("测试日志");
            FileLogHelper.DebugAdd("测试日志", "前缀");
            FileLogHelper.ErrorAdd("测试日志");
            FileLogHelper.ErrorAdd("测试日志", "前缀");

            Console.ReadKey();
        }

        /// <summary>
        /// 测试Redis
        /// </summary>
        [TestMethod]
        public void TestRedis()
        {
            RedisCacheBll redisCacheBll = new RedisCacheBll();
            DateTime now = DateTime.Now;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            FileLogHelper.DebugAdd($"开始新增Redis记录");
            for (int i = 0; i < 5; i++)
            {
                bool flag = redisCacheBll.Add(i.ToString(), new Test() { TestId = i, Name = "名字", CreateDate = now }, DateTime.Now.AddMinutes(5));
            }
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            FileLogHelper.DebugAdd($"新增Redis记录总共花费{ts2.TotalMilliseconds}ms.");

            Console.ReadKey();
        }

        /// <summary>
        /// 测试MemoryCache
        /// </summary>
        [TestMethod]
        public void TestMemoryCache()
        {
            MemoryCacheHelper.UpdateCache("222", "222", DateTime.Now.AddSeconds(10));
        }

    }
}
